/*
 * @Author: xiaosihan
 * @Date: 2024-10-20 08:08:12
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-10-20 17:45:54
 */

import adminApi from "@api/adminApi";
import { get } from "lodash";
import { observable, configure } from "mobx";
configure({ enforceActions: "never" });

const adminUserStore = observable(
  {
    isAsc: false, // 是否升序
    sortBy: "create_time", //排序字段
    pageNum: 1, //当前页面
    pageSize: 10, //分页数
    total: 0, //总数
    list: [] as Array<any>, //数据列表
    //请求数据
    async getList(
      param: {
        pageNum?: number;
        pageSize?: number;
        isAsc?: boolean;
        sortBy?: string;
      } = {}
    ) {
      this.pageNum = param.pageNum || this.pageNum;
      this.pageSize = param.pageSize || this.pageSize;
      this.isAsc = param.isAsc || this.isAsc;
      this.sortBy = param.sortBy || this.sortBy;

      const res = await adminApi.get_user_list({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        isAsc: this.isAsc,
        sortBy: this.sortBy,
      });

      this.list = get(res, ["data", "data"]) || [];
      this.pageNum = get(res, ["data", "pageNum"]) || 1;
      this.pageSize = get(res, ["data", "pageSize"]) || 10;
      this.total = get(res, ["data", "totalCount"]) || 0;
      
    },
    //新增数据
    add(data: any) {
      // 新增完后请求数据
      this.getList();
    },
    //删除数据
    delete(id: string) {
      // 删除完后请求数据
      this.getList();
    },
    //更新数据
    updata(data: any) {
      // 更新完后请求数据
      this.getList();
    },
  },
  {},
  { deep: false }
);
export default adminUserStore;
